﻿/*
=======================================================
    Stock【库存信息】
=======================================================
*/

namespace Services
{
    public interface IStockMapping : IMapping<Stock, StockModel, StockFilterModel>
    { }

    public class StockMapping : AutoStockMapping, IStockMapping
    {
        public StockMapping(ICurrentUser currentUser)
            : base(currentUser)
        {
        }

        #region 读数据
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entity">Entity对象</param>
        /// <returns></returns>
        public override StockModel Entity2Model(Stock entity)
        {
            if (entity == null)
            {
                return null;
            }

            StockModel model = base.Entity2Model(entity);


            /***************************************** 下面处理自定义转换 *****************************************/
            model.Department_Name = entity.Department_Name;

            return model;
        }
        #endregion

        #region 写数据
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="model">ViewModel对象</param>
        /// <returns></returns>
        public override Stock Model2Entity(StockModel model)
        {
            Stock entity = base.Model2Entity(model);


            /***************************************** 下面处理自定义转换 *****************************************/



            return entity;
        }
        #endregion

        #region 读数据(集合)
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entities">Entity对象</param>
        /// <returns></returns>
        public override List<StockModel> Entity2Model(IEnumerable<Stock> entities)
        {
            return entities.Select(x => Entity2Model(x)).ToList();
        }
        #endregion

        #region 写数据(集合)
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="models">ViewModel对象</param>
        /// <returns></returns>
        public override List<Stock> Model2Entity(IEnumerable<StockModel> models)
        {
            return models.Select(x => Model2Entity(x)).ToList();
        }
        #endregion

        #region 搜索条件转换
        /// <summary>
        /// 搜索条件转换，从FilterModel对象转换成Where对象
        /// </summary>
        /// <param name="filterModel">FilterModel对象</param>
        /// <returns></returns>
        public override Expressionable<Stock> FilterModel2Where(StockFilterModel filterModel)
        {
            var where = base.FilterModel2Where(filterModel);

            if (filterModel._IsSelect.HasValue && filterModel._IsSelect.Value == true)
            {
                //按矿山处理
                if (_currentUser.CurrentUserInfo.LoginUserType == LoginUserType.集团)
                {
                    if (_currentUser.CurrentUserInfo.LoginUserKuangShanDepartment.Item1 == false)
                    {
                        where = where.And(x => x.DepartmentID.HasValue && _currentUser.CurrentUserInfo.LoginUserKuangShanDepartment.Item2.Contains(x.DepartmentID.Value));
                    }
                }
            }
            else
            {
                //处理数据权限
                if (_currentUser.CurrentUserInfo.LoginUserType == LoginUserType.集团)
                {
                    where = where.And(x => _currentUser.CurrentUserInfo.LoginUserWarehouseIDsWithDataDepartment.Contains(x.WarehouseID));
                }
                else if (_currentUser.CurrentUserInfo.LoginUserType == LoginUserType.监管部门)
                {
                    where = where.And(x => x.DepartmentID.HasValue && _currentUser.CurrentUserInfo.LoginRegulatorDepartmentIDs.Contains(x.DepartmentID.Value));
                }
            }

            //SearchInput
            if (!string.IsNullOrEmpty(filterModel._SearchInput))
            { }

            ////规格型号
            //if (!string.IsNullOrEmpty(filterModel._GuiGe))
            //{
            //    where = where.And(x => x.GuiGe.Contains(filterModel._GuiGe));
            //    //where = where.And(x => x.GuiGe == filterModel._GuiGe);
            //}

            ////直径
            //if (filterModel._Diameter.HasValue)
            //{
            //    where = where.And(x => x.Diameter == filterModel._Diameter.Value);
            //}

            ////绳子条数
            //if (filterModel._Cnt.HasValue)
            //{
            //    where = where.And(x => x.Cnt == filterModel._Cnt.Value);
            //}

            ////公称捻距
            //if (filterModel._NianJu.HasValue)
            //{
            //    where = where.And(x => x.NianJu == filterModel._NianJu.Value);
            //}

            ////长度
            //if (filterModel._LengthNum.HasValue)
            //{
            //    where = where.And(x => x.LengthNum == filterModel._LengthNum.Value);
            //}

            ////备绳维保周期
            //if (filterModel._KeepDay.HasValue)
            //{
            //    where = where.And(x => x.KeepDay == filterModel._KeepDay.Value);
            //}


            return where;
        }
        #endregion
    }
}
